package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	http.HandleFunc("/health", healthz)
	http.HandleFunc("/", HandlerNow)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal(err)
	}
}

func healthz(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "这个应用是健康的")
}

func HandlerNow(w http.ResponseWriter, r *http.Request) {
	//接收客户端的request
	for key, val := range r.Header {
		for _, value := range val {
			w.Header().Add(key, value)
			fmt.Fprintf(w, "%s : %s\n", key, r.Header[key])
		}
	}
	version := os.Getenv("VERSION")
	//记录访问日志
	ip := r.RemoteAddr
	Code := http.StatusOK
	log.Printf("IP:%s , Code:%d\n", ip, Code)

	//设置响应码状态:
	w.WriteHeader(Code)
	w.Header().Set("server", version)
}
